package com.example.xjtuse_pc.services;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import com.example.xjtuse_pc.db.dao.AppLockDao;
import com.example.xjtuse_pc.mobilesafe.AppLock.EnterPwdActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 程序任务栈监控
 */
public class WatchDogService extends Service {
    private ScreenOffReceiver soffr;
    private ActivityManager am;
    private boolean flag;
    private AppLockDao ad;
    private InnerReceiver ir;
    private String temppackname;
    private DataChangeReceiver dc;
    private List<String> ls;
    private Intent i;

    public WatchDogService() {
    }

    private class ScreenOffReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            temppackname = null;


        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private class DataChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ls = ad.findAll();
        }
    }

    private class InnerReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("join", "收到停止广播");
            temppackname = intent.getStringExtra("packname");

        }
    }


    @Override
    public void onCreate() {
        soffr = new ScreenOffReceiver();
        registerReceiver(soffr, new IntentFilter(Intent.ACTION_SCREEN_OFF));
        ir = new InnerReceiver();
        registerReceiver(ir, new IntentFilter("com.example.xjtuse.moblesafe.tempstop"));
        dc = new DataChangeReceiver();
        registerReceiver(dc, new IntentFilter("com.example.xjtuse.lockchange"));
        ad = new AppLockDao(this);
        flag = true;
        ls = ad.findAll();
        am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        i = new Intent(WatchDogService.this, EnterPwdActivity.class);
        //此服务没有任务栈信息
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    List<ActivityManager.RunningTaskInfo> infos = am.getRunningTasks(1);
                    String packname = infos.get(0).topActivity.getPackageName();
                    //   if(ad.find(packname)){
                    if (ls.contains(packname)) {
                        if (packname.equals(temppackname)) {
                        } else {
                            i.putExtra("packname", packname);
                            startActivity(i);
                        }
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        dc = null;
        unregisterReceiver(dc);
        flag = false;
        unregisterReceiver(ir);
        ir = null;
        unregisterReceiver(soffr);
        soffr = null;
        super.onDestroy();
    }
}
